﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tamlytre
{
    public partial class Default : System.Web.UI.MasterPage
    {
        DBTuVanDataContext db = new DBTuVanDataContext();//khai báo lớp DataContext
        public void LoadNewstHay()
        {
            var newshay = (from n in db.TB_News
                          where n.BaiHay.Value == true && n.DaXoa == false && n.Status == 1
                          orderby n.NumViews descending
                          select n).Take(10);
            UC_BaiVietHay1.ListNewsHay = newshay.ToList();
            //UC_BaiVietHay1.ListNewsHay = db.TB_News.Where(n => n.BaiHay.Value == true && n.DaXoa == false && n.Status == 1).Take(10).ToList();
        }
        public void LoadNewsMostViews()
        {
            var mostnews = (from n in db.TB_News
                           where n.DaXoa == false && n.Status == 1
                           orderby n.NumViews descending
                           select n).Take(10);
            UC_BaiVietDuocViewNhieuNhat1.ListMostView = mostnews.ToList();
        }
        public void LoadMenuTop()
        {
            List<TB_Catagory> menutop = db.USP_Menutop().ToList();//Gọi đối tượng menutop đưa vào list
            rptmenutop.DataSource = menutop;//đổ vào Repeater
            rptmenutop.DataBind();
        }
        public void LoadMainMenu()
        {
            List<TB_Catagory> mainmenu = db.USP_MainMenuParent().ToList();
            rptMainMenuParent.DataSource = mainmenu;
            rptMainMenuParent.DataBind();
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadMenuTop();
                LoadMainMenu();
            }
            LoadNewstHay();
            LoadNewsMostViews();
        }

        protected void rptMainMenuParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hrId = (HiddenField)e.Item.FindControl("hrID"); //--- day la Id cua loai san pham A---

            Repeater rpChildrent = e.Item.FindControl("rptMainMenuChild") as Repeater;

            if (hrId != null && rpChildrent != null)
            {
                int id = Convert.ToInt32(hrId.Value.ToString().Trim());
                List<TB_Catagory> menuchild = db.USP_MainMenuChild(id).ToList();
                rpChildrent.DataSource = menuchild;
                rpChildrent.DataBind();
            }
        }

    }
}